package com.fenghongzhang.day008.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.fenghongzhang.day008.MainActivity;
import com.fenghongzhang.day008.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class ThreeFragment extends Fragment {
    private TextView time;
    private Button send;
    private int index = 5;

    private Timer timer;

    public Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 110){
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //子线程
                       handler.sendEmptyMessage(111);

                    }
                },0,1000);
            }
            else if(msg.what == 111){
                //主线程
                time.setText("倒计时:"+(index--)+"秒");
                if(index == 0){
                    //跳转
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);
                    //结束欢迎页
                    getActivity().finish();
                    timer.cancel();
                }
            }
        }
    };
    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_three, container, false);
        time = (TextView) inflate.findViewById(R.id.time);
        send = (Button) inflate.findViewById(R.id.send);


//        time.setText("")

        //点击
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                //结束欢迎页
                getActivity().finish();
                //取消定时器
                timer.cancel();
            }
        });


        return inflate;





    }
}
